package com.accountant.travelaccountant;

import java.util.ArrayList;
import java.util.HashMap;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;

public class ShowDetails extends Activity {
	public ArrayList<HashMap<Integer, String>> arrHashMap;
	public ArrayList<String> memberNames, amountsPaid;
	public HashMap<Integer, String> hashMap;
	ListView show_detail_listview;
	ShowDetails_ListViewAdapter details_listviewadapter;
	DatabaseOperations_NonActivity databaseOperations;
	Button btnWhoPaysWhom;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		arrHashMap = new ArrayList<HashMap<Integer, String>>();
		databaseOperations = new DatabaseOperations_NonActivity(this);

		setContentView(R.layout.show_detail_custom_list);

		show_detail_listview = (ListView) findViewById(R.id.show_detail_listview);
		populateHashMapForListView();
		details_listviewadapter = new ShowDetails_ListViewAdapter(this,
				arrHashMap);
		show_detail_listview.setAdapter(details_listviewadapter);
		
		
		/*Seting the button*/
		btnWhoPaysWhom=(Button)findViewById(R.id.WhoPaysWhomShowDetails);
		btnWhoPaysWhom.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent=new Intent(ShowDetails.this,ResultScreen2.class);
				startActivity(intent);
			}
		});
		
	}

	@SuppressLint("UseSparseArrays")
	private void populateHashMapForListView() {
		hashMap = new HashMap<Integer, String>();
		hashMap.put(0, "Member");
		hashMap.put(1, "Amount Paid");
		arrHashMap.add(hashMap);

		hashMap = new HashMap<Integer, String>();
		hashMap.put(0, "");
		hashMap.put(1, "");
		arrHashMap.add(hashMap);

		/* Opening the database */
		databaseOperations.openWriteableDatabase();
		/* Getting the list of all members */
		memberNames = databaseOperations.retrieveAll(
				DatabaseOperations_NonActivity.getMembertable(),
				new String[] { DatabaseOperations_NonActivity.getMembertablemember() },
				DatabaseOperations_NonActivity.getMembertabletitle() + "=\""
						+ SessionClass_NonActivity.sessionObject().getTitle() + "\"", null,
				null, null, null);
		/* Getting the list of all amounts */
		amountsPaid = databaseOperations.retrieveAll(
				DatabaseOperations_NonActivity.getMembertable(),
				new String[] { DatabaseOperations_NonActivity.getMembertableamount() },
				DatabaseOperations_NonActivity.getMembertabletitle() + "=\""
						+ SessionClass_NonActivity.sessionObject().getTitle() + "\"", null,
				null, null, null);

		/* Closing the database */
		databaseOperations.closeDatabase();

		/* Populating the hash map with every entry */
		for (int i = 0; i < memberNames.size(); i++) {
			hashMap = new HashMap<Integer, String>();
			hashMap.put(0, memberNames.get(i));
			hashMap.put(1, amountsPaid.get(i));
			arrHashMap.add(hashMap);
		}

	}

}
